package com.example.opengldemo.simpleGame.object;


import android.opengl.GLSurfaceView;

import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.simpleGame.MatrixState.MatrixState3D;
import com.example.opengldemo.simpleGame.util.manager.ShaderManager;

public class Cube {

    TexRect rect;
    float halfSize;

    RigidBody body;
    GLSurfaceView mv;
    // 立方体
    public Cube(GLSurfaceView mv, float halfSize, RigidBody body) {
        this.mv = mv;
        this.halfSize = halfSize;
        rect = new TexRect(mv, ShaderManager.getShader(0), 1, halfSize, halfSize);
        this.body = body;
    }

    public void drawSelf(int texId) {

        //绘制 保存变换矩阵
        MatrixState3D.pushMatrix();
        // 获取到 状态差不多是个计算的 东西
        Transform trans = body.getMotionState().getWorldTransform(new Transform());
        MatrixState3D.translate(trans.origin.x, trans.origin.y, trans.origin.z);
        trans.getOpenGLMatrix(MatrixState3D.getMMatrix());


        MatrixState3D.pushMatrix();
        MatrixState3D.translate(0, halfSize, 0);
        MatrixState3D.rotate(-90, 1, 0, 0);
        rect.drawSelf(texId);
        MatrixState3D.popMatrix();


        MatrixState3D.pushMatrix();
        MatrixState3D.translate(0, -halfSize, 0);
        MatrixState3D.rotate(90, 1, 0, 0);
        rect.drawSelf(texId);
        MatrixState3D.popMatrix();


        MatrixState3D.pushMatrix();
        MatrixState3D.translate(-halfSize, 0, 0);
        MatrixState3D.rotate(-90, 0, 1, 0);
        rect.drawSelf(texId);
        MatrixState3D.popMatrix();


        MatrixState3D.pushMatrix();
        MatrixState3D.translate(halfSize, 0, 0);
        MatrixState3D.rotate(90, 0, 1, 0);
        rect.drawSelf(texId);
        MatrixState3D.popMatrix();


        MatrixState3D.pushMatrix();
        MatrixState3D.translate(0, 0, halfSize);
        rect.drawSelf(texId);
        MatrixState3D.popMatrix();


        MatrixState3D.pushMatrix();
        MatrixState3D.translate(0, 0, -halfSize);
        MatrixState3D.rotate(180, 0, 1, 0);
        rect.drawSelf(texId);
        MatrixState3D.popMatrix();

        MatrixState3D.popMatrix();
    }
}
